using Framework;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Text;
using UnityEditor.Build;

namespace FrameworkEditor
{
    public class AndroidBuildTool
    {
        private static int buildOperations;
        private static bool forceBuildAssets;
        private static BuildOptions unityBuildOptions;
        private static SDKType buildSdkType;
        private static string BuildSdkName => buildSdkType.ToString().ToLower();
        private static string resVersionStr;
        private static string voiceBranchStr;
        private static string appVersionStr;
        private static int appBuildIdInt;
        private static string bundleIdStr;
        private static bool useMonoBackend;
        private static bool isDebugBuild;
        private static bool isUseProfile;
        private static bool startFromCI;
        private static readonly string NativeProjRootDirName = "androidproj";
        private static readonly string ExportProjDirName = "asproj";
        private static readonly string NativeProjRootDirPath = $"{Application.dataPath}/../../{NativeProjRootDirName}";
        public static readonly string ExportProjPath = $"{NativeProjRootDirPath}/{ExportProjDirName}";
        private static readonly string SrcIl2cppMetaDataPath = $"{ExportProjPath}/unityLibrary/src/main/assets/bin/Data/Managed/Metadata/global-metadata.dat";
        private static readonly string DstIl2cppMetaDataPath = $"{ExportProjPath}/unityLibrary/src/main/assets/bin/Data/Managed/Resources/MobileVideo.dll-resources.dat";
        private static readonly string Il2cppSourceDir = $"{Application.dataPath}/../Temp/StagingArea/Il2Cpp";
        private static readonly string Il2cppExportDir = $"{NativeProjRootDirPath}/Il2Cpp_Export";

        private static readonly string[] CopyAssetsDirList = new string[]
        {
            "local.properties",
            "unityLibrary/build.gradle",
            "unityLibrary/proguard-unity.txt",
            "unityLibrary/libs",
            "unityLibrary/src/main/assets",
            "unityLibrary/src/main/java",
            "unityLibrary/src/main/jniLibs",
            "launcher/src/main/res/values/strings.xml"
        };

        private static string NativeProjDirPath => $"{NativeProjRootDirPath}/{BuildSdkName}";
        private static string AppGradlePath => $"{NativeProjRootDirPath}/{BuildSdkName}/launcher/build.gradle";
        private static string ApkPath => $"{NativeProjRootDirPath}/{BuildSdkName}/launcher/build/outputs/apk/" + (isDebugBuild ? "debug" : "release");

        public static void Build(bool rebuildAssets)
        {
            forceBuildAssets = rebuildAssets;
            BuildAssets();
        }
        
        public static void BuildAssets()
        {
            if (startFromCI)
            {
                return;
            }
            Log("start build assets");
            AssetsBuilder.StartBuild(FrameworkSettings.AndroidPlayer, forceBuildAssets, false);
            Log("end build assets");
        }
        private static void Log(string log)
        {
            UnityEngine.Debug.Log(log);
        }
    }
}